package cn.sdhuijin.healthexam.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 数据导出检索
 * 
 * @author sunping
 * 
 */
public class ExportDataActivity extends AbstractControlActivity implements
		OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_export_data);
		ExitApplication.getInstance().addActivity(this);

		setEditTextListener(R.id.edt_begin_date);
		setEditTextListener(R.id.edt_end_date);
		this.setButtonListener(this, R.id.btn_back, R.id.btn_view,
				R.id.btn_setting);

	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.btn_back:
			this.finish();
			break;
		case R.id.btn_view:
			intent.putExtra("sql", getSearchSql());
			intent.setClass(this, ExportDateListActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_setting:
			intent.setClass(this, ExportSettingActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	/**
	 * 拼接查询sql语句
	 * 
	 * @return
	 */
	public String getSearchSql() {
		Log.i(this.getClass().getSimpleName(), "搜索SQL语句----start");
		// select t2.姓名 AS 姓名, t2.身份证号 AS 身份证号,t2.性别 AS 性别,t2.出生日期 AS
		// 出生日期,t1.表格类型 AS 类型 ,t1.医务人员 AS 医生,t1.录入日期 AS 录入日期
		// from 索引表 t1 LEFT JOIN 身份证表 t2 ON t1.身份证号 = t2.身份证号 where t2.姓名 = ？
		// AND t2.身份证号 = ？
		// AND t2.性别 = ? AND t2.出生日期 = ? AND t1.医务人员 = ? AND t1.录入日期 > ? AND
		// t1.录入日期 < ? AND t1.表格类型 in (?) AND t1.是否完成 = '是'
		StringBuffer sql = new StringBuffer(
				"SELECT t2.姓名 AS 姓名, t2.身份证号 AS 身份证号,t2.性别 AS 性别,t2.出生日期 AS 出生日期,t1.表格类型 AS 类型 ,t1.医务人员 AS 医生,t1.录入日期 AS 录入日期 FROM 索引表 t1 LEFT JOIN 身份证表 t2 ON t1.身份证号 = t2.身份证号  WHERE ");

		String doctor = this.getControlValue(R.id.edt_doctor); // 获取责任医生
		if (Util.allIsNotEmpty(doctor)) {
			sql.append("t1.医务人员= '" + doctor + "' AND ");
		}

		String beginDate = this.getControlValue(R.id.edt_begin_date); // 获得开始日期
		String endDate = this.getControlValue(R.id.edt_end_date); // 获得结束日期

		if (Util.allIsNotEmpty(beginDate, endDate)) {
			beginDate = Util.dateFormat(beginDate) + "000000";
			endDate = Util.dateFormat(endDate) + "000000";
			sql.append("t1.录入日期  >= '" + beginDate + "' AND ");
			sql.append("t1.录入日期  < '" + endDate + "' AND ");
		}

		boolean archivesChecked = this.isCheckBoxChecked(R.id.check_export_doc);
		boolean examinationChecked = this
				.isCheckBoxChecked(R.id.check_export_exam);
		boolean visitChecked = this.isCheckBoxChecked(R.id.check_export_visit);
		boolean hypertensionVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_hypertension_visit);
		boolean mentalVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_mental_visit);
		boolean doctorJudgeChecked = this
				.isCheckBoxChecked(R.id.check_export_zhongyi_visit);
		boolean chanQianFirstChecked = this
				.isCheckBoxChecked(R.id.check_export_chanqianfirst_visit);
		boolean chanQianSecondChecked = this
				.isCheckBoxChecked(R.id.check_export_chanqiansecond_visit);
		boolean chanQianThirdChecked = this
				.isCheckBoxChecked(R.id.check_export_chanqianthird_visit);
		boolean chanQianForthChecked = this
				.isCheckBoxChecked(R.id.check_export_chanqianforth_visit);
		boolean chanQianFifthChecked = this
				.isCheckBoxChecked(R.id.check_export_chanqianfifth_visit);
		boolean chanHouChecked = this
				.isCheckBoxChecked(R.id.check_export_chanhou_visit);
		boolean chanHouSiShiErChecked = this
				.isCheckBoxChecked(R.id.check_export_chanhousishier_visit);
		boolean NewChildVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_visit);
		boolean NewChildManYueVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_manyue_visit);
		boolean NewChildSanYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_export_xinshenger_sanyue_visit);
		boolean NewChildLiuYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_export_xinshenger_liuyue_visit);
		boolean NewChildBaYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_export_xinshenger_bayue_visit);
		boolean NewChildShiErYueVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_shieryue_visit);
		boolean NewChildShiBaYueVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_shibayue_visit);
		boolean NewChildErShiSiYueVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_ershisiyue_visit);
		boolean NewChildSanShiYueVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_sanshiyue_visit);
		boolean NewChildSanSuiVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_sansui_visit);
		boolean NewChildSiSuiVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_sisui_visit);
		boolean NewChildWuSuiVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_wusui_visit);
		boolean NewChildLiuSuiVisitChecked = this
				.isCheckBoxChecked(R.id.check_export_xinshenger_liusui_visit);

		if (archivesChecked || examinationChecked || visitChecked
				|| mentalVisitChecked || doctorJudgeChecked
				|| chanQianFirstChecked || chanQianSecondChecked
				|| chanQianThirdChecked || chanQianForthChecked
				|| chanQianFifthChecked || chanHouChecked
				|| chanHouSiShiErChecked || NewChildVisitChecked
				|| NewChildManYueVisitChecked || NewChildSanYueVisitChecked
				|| NewChildLiuYueVisitChecked || NewChildBaYueVisitChecked
				|| NewChildShiErYueVisitChecked || NewChildShiBaYueVisitChecked
				|| NewChildErShiSiYueVisitChecked
				|| NewChildSanShiYueVisitChecked || NewChildSanSuiVisitChecked
				|| NewChildSiSuiVisitChecked || NewChildWuSuiVisitChecked
				|| NewChildLiuSuiVisitChecked) {
			sql.append("t1.表格类型 in (");
			if (archivesChecked) {
				sql.append("'档案',"); // 档案类型
			}
			if (examinationChecked) {
				sql.append("'体检',"); // 体检类型
			}
			if (visitChecked) {
				sql.append("'糖尿病随访',"); // 糖尿病随访类型
			}
			if (hypertensionVisitChecked) {
				sql.append("'高血压随访',"); // 高血压随访类型
			}
			if (mentalVisitChecked) {
				sql.append("'精神病随访',"); // 精神病随访类型
			}
			if (doctorJudgeChecked) {
				sql.append("'中医体质判定',"); // 中医体质判定
			}
			if (chanQianFirstChecked) {
				sql.append("'产前第一次随访',"); // 产前第一次随访
			}
			if (chanQianSecondChecked) {
				sql.append("'产前第二次随访',"); // 产前第二次随访
			}
			if (chanQianThirdChecked) {
				sql.append("'产前第三次随访',"); // 产前第三次随访
			}
			if (chanQianForthChecked) {
				sql.append("'产前第四次随访',"); // 产前第四次随访
			}
			if (chanQianFifthChecked) {
				sql.append("'产前第五次随访',"); // 产前第五次随访
			}
			if (chanHouChecked) {
				sql.append("'产后随访',"); // 产后随访
			}
			if (chanHouSiShiErChecked) {
				sql.append("'产后四十二天随访',"); // 产后四十二天随访
			}
			if (NewChildVisitChecked) {
				sql.append("'新生儿随访',"); // 新生儿随访
			}
			if (NewChildManYueVisitChecked) {
				sql.append("'新生儿满月随访',"); // 新生儿满月随访
			}
			if (NewChildSanYueVisitChecked) {
				sql.append("'新生儿三月龄随访',"); // 新生儿三月随访
			}
			if (NewChildLiuYueVisitChecked) {
				sql.append("'新生儿六月龄随访',"); // 新生儿六月龄随访
			}
			if (NewChildBaYueVisitChecked) {
				sql.append("'新生儿八月龄随访',"); // 新生儿八月龄随访
			}
			if (NewChildShiErYueVisitChecked) {
				sql.append("'新生儿十二月龄随访',"); // 新生儿十二月龄随访
			}
			if (NewChildShiBaYueVisitChecked) {
				sql.append("'新生儿十八月龄随访',"); // 新生儿十八月龄随访
			}
			if (NewChildErShiSiYueVisitChecked) {
				sql.append("'新生儿二十四月龄随访',"); // 新生儿二十四月龄随访
			}
			if (NewChildSanShiYueVisitChecked) {
				sql.append("'新生儿三十月龄随访',"); // 新生儿二十四月龄随访
			}
			if (NewChildSanSuiVisitChecked) {
				sql.append("'新生儿三岁龄随访',"); // 新生儿三岁龄随访
			}
			if (NewChildSiSuiVisitChecked) {
				sql.append("'新生儿四岁龄随访',"); // 新生儿四岁龄随访
			}
			if (NewChildWuSuiVisitChecked) {
				sql.append("'新生儿五岁龄随访',"); // 新生儿五岁龄随访
			}
			if (NewChildLiuSuiVisitChecked) {
				sql.append("'新生儿六岁龄随访',"); // 新生儿六岁龄随访
			}
			// 截取掉最后一个逗号
			if ((sql.length() > 0) && (',' == sql.charAt(sql.length() - 1)))
				sql = sql.deleteCharAt(sql.length() - 1);
			sql.append(") AND ");
		}
		sql.append(" t1.是否完成 = '是'");
		String searchSql = new String(sql);
		Log.i(this.getClass().getSimpleName(), "sql查询语句---" + sql);
		return searchSql;
	}

}
